#!/bin/bash

source /home/shell/util.sh



# 安装函数
function install () {
    if [ "$(ostype)" == "Ubuntu" ]; then
        apt install -y libbz2-dev libxml2 libxml2-dev libsqlite3-dev zlib1g-dev libssl-dev libcurl4-openssl-dev libpng-dev libjpeg-dev libfreetype6-dev libgmp3-dev libonig-dev
    else
        echo "centos 未实现"
        exit
    fi
    while [ true ]; do
        printInfo "版本列表："
        curl -sL https://www.php.net/downloads.php|grep "tar.gz"|perl -pe "s/.*tar.gz\">(.*?)<\/a>.*/\1/g";
        # curl -sL http://php.p2hp.com/downloads.php|grep "tar.gz"|perl -pe "s/.*tar.gz\">(.*?)<\/a>.*/\1/g";
        read -p "请选择版本：" version
        if [[ -z "${version}" ]]; then
        printFail "请输入版本"
        else
        break;
        fi
    done
    # download /usr/local/src/php.tar.gz https://mirrors.sohu.com/php/${version}
    download /usr/local/src/php.tar.gz https://www.php.net/distributions/${version}
    tar -xf /usr/local/src/php.tar.gz -C /usr/local/src
    rm -rf /usr/local/src/php.tar.gz
    mv /usr/local/src/php-* /usr/local/src/php
    cd /usr/local/src/php
    # ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www \
    # --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --with-iconv-dir \
    # --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath \
    # --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl \
    # --with-mcrypt --with-libmbfl --enable-ftp --with-gd --enable-gd-jis-conv --enable-gd-native-ttf --with-openssl --with-mhash \
    # --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache \
    # --with-pear --enable-maintainer-zts --with-ldap=shared --without-gdbm
    ./configure --with-bz2 \
                --with-mysqli \
                --with-pdo-mysql \
                --enable-mysqlnd \
                --with-openssl \
                --with-zlib \
                --with-curl \
                --enable-gd \
                --with-jpeg \
                --with-freetype \
                --with-gettext \
                --with-mhash \
                --enable-mbstring \
                --with-gmp \
                --enable-soap \
                --enable-ftp \
                --enable-sockets \
                --enable-fpm

    make && make install
}

# 配置函数
function config(){

    cp -r /usr/local/src/php/php.ini-development /usr/local/php/php.ini
    cp -r /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
    cp -r /usr/local/src/php/sapi/fpm/php-fpm /usr/local/bin
    cp -r /usr/local/src/php/sapi/fpm/php-fpm.conf /usr/local/etc/

    sed -i "s/user = nobody/user = www-data/g" /usr/local/etc/php-fpm.d/www.conf
    sed -i "s/group = nobody/group = www-data/g" /usr/local/etc/php-fpm.d/www.conf
    sed -i "s#include=NONE/etc/php-fpm.d/\*.conf#include=etc/php-fpm.d/*.conf#g" /usr/local/etc/php-fpm.conf
    rm -rf /usr/local/src/php/
    composer config -g repo.packagist composer https://repo.huaweicloud.com/repository/php/
}

function run () {
bash /home/shell/serviced-install.sh<<EOF
2


php-fpm
/usr/local/sbin/php-fpm --daemonize




EOF
}

# 运行安装和配置
install && config && run

printInfo "========================php========================"
php -v
printInfo "========================php========================"